/*#include <stdio.h>
int main()
{
	int t,n, i, j, flag;
	scanf("%d", &t);
	int arr[2][3];
		while(t>0)
		{
			scanf("%d", &n);
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < n; j++)
			{
				scanf("%d", &arr[i][j]);
			}
		}
		flag=1;	
		for (i = 1; i < n ; i++)
		{	
			for (j = 0; j <i; j++)
			{						
				if (arr[i][j] != 0)
				{
					flag = 0;
					break;
				}
			}
		}
		if (flag != 0)
		{
			printf("YES");
		}
		else{
			printf("NO"); 
		} 
		printf("\n");
			t--;
	}
	return 0;
}
#include <stdio.h>
int main()
{
	int m, n,i,j,j1;
	scanf("%d %d", &m, &n);
	int arr[100][100], arr1[100][100];
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			scanf("%d", &arr[i][j]);
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			if (m <= n)
			{
				if (j + m <= n - 1)
				{
					arr1[i][j + m] = arr[i][j];
				}
				else
				{
					arr1[i][j + m - n] = arr[i][j];
				}
			}
			else
			{
				if (j + m - n <= n)
				{
					arr1[i][j + m - n] = arr[i][j];
				}
				else
				{
					j1=j+m-n
					while(j1<=n)
					{
						j1=j1-n;
					}
					arr1[i][j + m - n-n] = arr[i][j];
				}
			}
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			printf("%d ", arr1[i][j]);
		}
		printf("\n");
	}
	return 0;
}
#include <stdio.h>
int main()
{
    int m, n, i, j, flag = 1; int max = 0, min = 0, count = 0, index=0, index1=0;
    scanf("%d %d", &m, &n);
    int arr[100][100];
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    for (i = 0; i < m; i++)
    {
        max = arr[i][0];
        for (j = 0; j < n; j++)
        {
            if (arr[i][j] >= max)
            {
                max = arr[i][j];
                index = j;
                index1 = i;
            }
        }
        if (m == 1)
        {
            count++;
            printf("Array[%d][%d] = %d", index1, index, arr[index1][index]);
            break;
        }
        for (int k = 0; k < m; k++)
        {
            min = arr[0][index];
            if (arr[k][index] <= min)
            {
                min = arr[k][index];
    			index1 = k;
            }
        }
        if (n == 1)
        {
            count++;
            printf("Array[%d][%d] = %d", index1, index, min);
            break;
        }
        if (m != 1 && n != 1 && min == max)
        {
            count++;
            printf("Array[%d][%d] = %d", index1, index, arr[index1][index]);
            break;
        }
    }
    if (count == 0)
    {
        printf("None");
    }
    return 0;
}*/
#include <stdio.h>
int main()
{
	int m, n,i,j,j1;
	scanf("%d %d", &m, &n);
	int arr[100][100], arr1[100][100];
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			scanf("%d", &arr[i][j]);
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			m=m%n;
			{
				if (j + m <= n - 1)
				{
					arr1[i][j + m] = arr[i][j];
				}
				else
				{
					arr1[i][j + m - n] = arr[i][j];
				}
			}	
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			printf("%d ", arr1[i][j]);
		}
		printf("\n");
	}
	return 0;
}




